/**
 * 
 */
package com.base.myproject.client.busi.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Vector;

import com.base.myproject.client.busi.BaseStore;
import com.base.myproject.client.busi.BO.BusiObject;
import com.base.myproject.client.jdbc.StoreInfo;
import com.base.myproject.client.tools.SendServlet;
import com.base.myproject.client.ui.BaseBusiLeftPanel;
import com.base.myproject.client.ui.BaseFormPanel;
import com.base.myproject.client.ui.BaseJDBCPanel;
import com.base.myproject.client.ui.celleditor.BaseComboBoxForm;
import com.base.myproject.client.ui.codenamemap.CodeNameMapFactory;
import com.extjs.gxt.ui.client.Style.HorizontalAlignment;
import com.extjs.gxt.ui.client.Style.LayoutRegion;
import com.extjs.gxt.ui.client.Style.VerticalAlignment;
import com.extjs.gxt.ui.client.data.BaseModelData;
import com.extjs.gxt.ui.client.data.ModelType;
import com.extjs.gxt.ui.client.event.BaseEvent;
import com.extjs.gxt.ui.client.event.Events;
import com.extjs.gxt.ui.client.event.FieldEvent;
import com.extjs.gxt.ui.client.event.Listener;
import com.extjs.gxt.ui.client.util.Margins;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.HorizontalPanel;
import com.extjs.gxt.ui.client.widget.Info;
import com.extjs.gxt.ui.client.widget.MessageBox;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.form.CheckBox;
import com.extjs.gxt.ui.client.widget.form.DateField;
import com.extjs.gxt.ui.client.widget.form.Field;
import com.extjs.gxt.ui.client.widget.form.TextField;
import com.extjs.gxt.ui.client.widget.form.Validator;
import com.extjs.gxt.ui.client.widget.grid.ColumnConfig;
import com.extjs.gxt.ui.client.widget.grid.ColumnModel;
import com.extjs.gxt.ui.client.widget.layout.BorderLayoutData;
import com.extjs.gxt.ui.client.widget.layout.TableData;
import com.extjs.gxt.ui.client.widget.layout.TableLayout;
import com.google.gwt.core.client.GWT;
import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Label;

/**
 * 美容美发的用户的登录
 * @author zgw
 *2009-10-25 下午05:53:33
 * javazgw@gmail.com
 */
public class BcodePanel extends BaseJDBCPanel implements Listener<BaseEvent> {

	Button addbutton ;
	Button delbutton ;
	Button savebutton;
	TextField<String> ccode_t ;
	TextField<String> bname_t ;
	BaseComboBoxForm<BaseModelData> sex_t ;
	TextField<String> edu_t ;
	DateField bird_t ;
	DateField incom_t ;
	TextField<String> mobil_t ;
	TextField<String> idno_t ;
	TextField<String> liveaddr_t ;
	
	
	TextField<String>  shortcode_t;
	BaseBusiLeftPanel basbusileftpanel;
	
	BaseStore<BusiObject> store = new BaseStore<BusiObject>();
	
	public BaseComboBoxForm<BaseModelData> groupicode_t ;
	
	BaseComboBoxForm<BaseModelData> systemid_t;
	BaseFormPanel bfp;
	public String mainsql = "select * from bcode ";

	public BcodePanel()
	{
		super();
		init();
	}
	public void init()
	{
		this.setWidth(Window.getClientWidth());
		this.setAutoHeight(true);
		BorderLayoutData centerData = new BorderLayoutData(LayoutRegion.CENTER);  
	    centerData.setMargins(new Margins(5, 5, 0, 0));  
	    initx();
	    this.add(initWeigetx(),centerData);
		
		registerMainStore(getStore());
		registerStore(getStore());
		
		this.setBusino(busi);
		//this.setWidth("1024px");
//		this.setHeight("720px");
		//this.setHeight("600");
		
		setdefault();
	}
	/**
	 * 被之类覆盖
	 */
	void initx()
	{
		 groupicode_t = com.base.myproject.client.ui.codenamemap.CodeNameMapFactory.createGroupInput_F();
	}
	protected void setdefault()
	{
		//Env.get
		//getStore().addDefault("systemid", "21");
		getStore().addDefault("password", "666666");

	}
	private HorizontalPanel initWeigetx()
	{
		HorizontalPanel hpanel = new HorizontalPanel();
		hpanel.setVerticalAlign(VerticalAlignment.TOP);
		 hpanel.add(CreateCcodeLeftPanel());
		 hpanel.add(CreateBcodeForm());
		 
		 return hpanel;
	}

	 public ContentPanel CreateCcodeLeftPanel() {
			List<ColumnConfig> columns = new ArrayList<ColumnConfig>();
			ColumnConfig cname = new ColumnConfig("bcode", "编号", 50);
		

			columns.add(cname);
			columns.add(new ColumnConfig("bname", "名字", 50));

			ColumnModel cm = new ColumnModel(columns);

			// --set modetype
			ModelType type = new ModelType();
			type.setRoot("records");
			type.setRecordName("record");
			type.setTotalName("totle");
			type.addField("icode");
			type.addField("bcode");
			type.addField("bname");
			type.addField("departments");
			type.addField("password");
			type.addField("email");
			type.addField("sex");
			type.addField("edu");
			type.addField("bird");
			type.addField("incom");
			type.addField("mobil");
			type.addField("idno");
			type.addField("status");
		
			type.addField("liveaddr");
			type.addField("systemid");
			type.addField("bcodeshortcode");
			
			type.addField("groupicode");
			type.addField("canaddfinance");
			type.addField("candownload");
			type.addField("caneditccode");
			type.addField("canchangepo");
			
			type.getField("bird").setType(Date.class);
			type.getField("incom").setType(Date.class);
			//type.getField("systemid").setType(Integer.class);
			type.getField("sex").setType(Integer.class);
			type.getField("canaddfinance").setType(Boolean.class);
			type.getField("candownload").setType(Boolean.class);
			type.getField("caneditccode").setType(Boolean.class);
			type.getField("canchangepo").setType(Boolean.class);
			// type.addField("Phone");
			// type.addField("imagepath");
			StoreInfo info = new StoreInfo();
			info.setTablename("bcode");
			info.setSql(mainsql);
			info.setForcenolimit("true");
			info.setKeyname(new String[] { "icode" });
			 basbusileftpanel = new BaseBusiLeftPanel(columns,
					type, info);
			setStore(basbusileftpanel.getStore());
			//getStore().setKeyname(new String[] { "codename" });

			basbusileftpanel.getGrid().setAutoExpandColumn("codename");
			setMaincomponent(basbusileftpanel.getGrid());
			return basbusileftpanel.getPanel();
		}
	 BaseFormPanel CreateBcodeForm()
	{
		
		
		
		 bfp = new BaseFormPanel(getStore());
		bfp.setLabelWidth(100);
		bfp.setFieldWidth(100);
		Label ccode_l = new Label("编号");
		//ccode_l.setWidth("20");
		Label sex_l = new Label("性别");
	//	sex_l.setWidth("40");
		Label bname_l = new Label("名字");
		//bname_l.setWidth("40");

		Label edu_l = new Label("学历");
		Label bird_l = new Label("出生日期");
		Label incom_l = new Label("入司日期");
		Label mobil_l = new Label("手机号");
		Label idno_l = new Label("身份证号");
		Label liveaddr_l = new Label("居住地址");
		Label shortcode_l = new Label("简码");
		Label groupicode_l = new Label("用户所在组");
		Label systemid_l = new Label("所在公司/店面");
		
		 Validator v = new Validator(){
		
						@Override
						public String validate(Field<?> field, String value) {
						
						
							int len = value.length();
							if(!(len==1||len==4||len==7||len>7))
								return "录入的内容长度必须是 1 、4、7、或长度大于7。譬如录入1或者1000或者1000100这种格式都是正确的。录入的代码请按照公司制定的规则录入。";
							
							if(systemid_t.getValueCode()==null)
								return "请先录入“所在公司/店面”";
							GWT.log("--"+systemid_t.getValueCode(), null);
							if(!value.startsWith(systemid_t.getValueCode()))
							{
								return "编码必须以"+systemid_t.getValueCode()+"开头";
							}
							
							return null;
						}
						 
					 };
		
		 ccode_t = new TextField<String>();
		 ccode_t.setValidator(v);
		 ccode_t.setValidateOnBlur(true);
		 ccode_t.setAllowBlank(false);
		 bname_t = new TextField<String>();
		// sex_t = new TextField<String>();
		 sex_t = CodeNameMapFactory.createSexInput_F();
			
		 edu_t = new TextField<String>();
		 bird_t = new DateField();
		 
		 
		 bird_t.getPropertyEditor().setFormat(DateTimeFormat.getFormat("yyyy-MM-dd HH:mm:ss"));  
		 incom_t = new DateField();
		 incom_t.getPropertyEditor().setFormat(DateTimeFormat.getFormat("yyyy-MM-dd HH:mm:ss"));  
		 mobil_t = new TextField<String>();
		 idno_t = new TextField<String>();
		 liveaddr_t = new TextField<String>();
		 shortcode_t = new TextField<String>();
		 
		 groupicode_t.addListener(Events.Change, this);
		 groupicode_t.setAllowBlank(false);
		 
		 systemid_t =  com.base.myproject.client.ui.codenamemap.CodeNameMapFactory.createSysteminfoInput_F();
		 systemid_t.setAllowBlank(false);
		 
		 ccode_t.setName("bcode") ;
		 bname_t.setName("bname") ;
		 sex_t.setName("sex");
		 edu_t.setName("edu") ;
		 bird_t.setName("bird") ;
		 incom_t.setName("incom") ;
		 mobil_t.setName("mobil") ;
		 idno_t.setName("idno") ;
		 liveaddr_t.setName("liveaddr") ;
		 shortcode_t.setName("bcodeshortcode") ;
		 groupicode_t.setName("groupicode");
		 systemid_t.setName("systemid");
		 
		//this.setHeaderVisible(false);
		this.setHeight("200");
		TableLayout tl = new TableLayout(4);
		tl.setWidth("600");
		tl.setCellSpacing(10);
		
		bfp.setLayout(tl);
		TableData td=new TableData();
//		td.setMargin(20);
//		td.setPadding(20);
		td.setColspan(1);
	//	td.setWidth("600");
		td.setMargin(100);
		td.setPadding(5);
		td.setRowspan(1);
		
		
		bfp.add(systemid_l,td);
		bfp.add(systemid_t,td);
		bfp.add(ccode_l,td);
		bfp.add(ccode_t,td);
		bfp.add(bname_l,td);
		bfp.add(bname_t,td);
		bfp.add(shortcode_l,td);
		bfp.add(shortcode_t,td);
		
		bfp.add(sex_l,td);
		bfp.add(sex_t,td);
		bfp.add(edu_l,td);
		bfp.add(edu_t,td);
		bfp.add(bird_l,td);
		bfp.add(bird_t,td);
		bfp.add(incom_l,td);
		bfp.add(incom_t,td);
		bfp.add(mobil_l,td);
		bfp.add(mobil_t,td);
		bfp.add(idno_l,td);
		bfp.add(idno_t,td);
		bfp.add(liveaddr_l,td);
		bfp.add(liveaddr_t,td);
		 
		bfp.add(groupicode_l,td);
		bfp.add(groupicode_t,td);
		
	
	
//		 CheckBoxGroup checkGroup = new CheckBoxGroup();  
//		 checkGroup.setFieldLabel("Music");  
		 CheckBox check1 = new CheckBox();  
		 check1.setBoxLabel("允许给会员充值");  
		 check1.setName("canaddfinance");
		 check1.setToolTip("选择后可以有给会员充值的权限");
		 
		 CheckBox check2 = new CheckBox();  
		 check2.setBoxLabel("允许下载数据");  
		 check2.setName("candownload");
		 check2.setToolTip("选择后可以下载资料");
		 
		 CheckBox check3 = new CheckBox();  
		 check3.setBoxLabel("可以改会员信息");  
		 check3.setName("caneditccode");
		 check3.setToolTip("选择后有旧的会员信息进行修改的权限");
		 
		 
		 CheckBox check4 = new CheckBox();  
		 check4.setBoxLabel("可以改收单管理数据");  
		 check4.setName("canchangepo");
		 check4.setToolTip("选择后有对已经在“收单管理”里已经“收单”的数据进行修改的权限");
		// check2.setValue(true);  
//		 checkGroup.add(check2);
		 bfp.add(check1,td);
		 bfp.add(check2,td);
		 bfp.add(check3,td);
		 bfp.add(check4,td);
	
			
		
		HorizontalPanel hpanel = new HorizontalPanel();
		 addbutton = new Button("添加");
		 delbutton = new Button("删除");
		 savebutton = new Button("保存");
		
		hpanel.add(addbutton);
		hpanel.add(delbutton);
		hpanel.add(savebutton);
		addbutton.addListener(Events.Select, this);
		delbutton.addListener(Events.Select, this);
		savebutton.addListener(Events.Select, this);
		bfp.add(hpanel,td);
		bfp.inited();
		return bfp;
	}
	
	
	/**
	 * @return the store
	 */
	public BaseStore<BusiObject> getStore() {
		return store;
	}
	/**
	 * @param store the store to set
	 */
	public void setStore(BaseStore<BusiObject> store) {
		this.store = store;
	}
//	void reset()
//	{
//		 ccode_t.setValue("") ;
//		 bname_t.setValue("") ;
//		 sex_t.setValue("");
//		 edu_t.setValue("") ;
////		 bird_t.setValue("") ;
////		 incom_t.setValue("") ;
//		 mobil_t.setValue("") ;
//		 idno_t.setValue("") ;
//		 liveaddr_t.setValue("") ; 
//	}
	protected void save()
	{
		if(bfp.isValid())
		{
		store.trySave();
		}
		else
		{
			MessageBox.alert("错误", "有必录项没有录入。数据还不能被保存！",null);
		}
	}
	@Override
	public void handleEvent(BaseEvent be) {
		
		if(be.getSource().equals(addbutton))
		{
//			basbusileftpanel.getGrid().addnewrecord();
			addnew();
		}
		else if(be.getSource().equals(delbutton))
		{
			basbusileftpanel.getGrid().deleterecord();
		}
		else if(be.getSource().equals(savebutton))
		{
//			getStore().trySave();
			save();
		}
		
		if (be.getType() == Events.Change)
		{
			
			String name = ((FieldEvent) be).getField().getName();
			
			if (name.equals("groupicode")) {
				
//				ccode_t.setFireChangeEventOnSetValue(true);
//				ccode_t.setValue(icode_t.getValue());
//				ccode_t.setFireChangeEventOnSetValue(false);
				if(ccode_t.getValue()==null ||ccode_t.getValue().trim().equals(""))
				{
					MessageBox.alert("警告", "请先录入编号", null);
					return;
				}
				if(groupicode_t.getValue().get("code")==null ||groupicode_t.getValue().get("code").equals(""))
				{
					MessageBox.alert("警告", "请先录入  “用户所在组”", null);
					return;
				}
				
				String ccode = ccode_t.getValue();
				String groupicode = groupicode_t.getValue().get("code");
				SendServlet ss = new SendServlet();
				groupicode_t.setFireChangeEventOnSetValue(true);
				String sql = "insert into basegroupmenber (bcode,groupicode) values('"+ccode+"','"+groupicode+"')";
				String deletesql ="delete basegroupmenber where groupicode!='"+groupicode+"' and bcode='"+ccode+"'";
				
				Vector v = new Vector();
				v.add(sql);
				v.add(deletesql);
				ss.ExcJDBCSQL(v, new AsyncCallback<String>(){

					@Override
					public void onFailure(Throwable caught) {
						
						Info.display("提示","设置组失败");
					}

					@Override
					public void onSuccess(String result) {
						
						Info.display("提示","设置组成功");
						store.trySave();
					}
					
				});
				
				groupicode_t.setFireChangeEventOnSetValue(false);
				
			} 
		}
	}
	
	protected void addnew()
	{
		basbusileftpanel.getGrid().addnewrecord();
	}
}
